#!/bin/bash

echo "=== API测试脚本 ==="

BASE_URL_ORDER="http://localhost:8080"
BASE_URL_INVENTORY="http://localhost:8081"

# 测试创建订单
echo "1. 测试创建订单..."
ORDER_RESPONSE=$(curl -s -X POST $BASE_URL_ORDER/api/orders \
  -H "Content-Type: application/json" \
  -d '{
    "orderId": "ORDER_001",
    "customerId": "CUSTOMER_001",
    "items": [
      {
        "productId": "PRODUCT_001",
        "productName": "商品A",
        "quantity": 2,
        "unitPrice": 100.0
      },
      {
        "productId": "PRODUCT_002",
        "productName": "商品B",
        "quantity": 1,
        "unitPrice": 50.0
      }
    ],
    "totalAmount": 250.0
  }')

echo "创建订单响应: $ORDER_RESPONSE"
echo ""

# 等待事件处理
echo "2. 等待事件处理..."
sleep 3

# 查看库存状态
echo "3. 查看库存状态..."
INVENTORY_RESPONSE=$(curl -s $BASE_URL_INVENTORY/api/inventory/status)
echo "库存状态: $INVENTORY_RESPONSE"
echo ""

# 测试支付订单
echo "4. 测试支付订单..."
PAY_RESPONSE=$(curl -s -X POST $BASE_URL_ORDER/api/orders/ORDER_001/pay \
  -H "Content-Type: application/json" \
  -d '{
    "paymentId": "PAY_001",
    "paymentMethod": "ALIPAY",
    "amount": 250.0
  }')

echo "支付订单响应: $PAY_RESPONSE"
echo ""

# 等待事件处理
echo "5. 等待事件处理..."
sleep 3

# 再次查看库存状态
echo "6. 再次查看库存状态..."
INVENTORY_RESPONSE2=$(curl -s $BASE_URL_INVENTORY/api/inventory/status)
echo "支付后库存状态: $INVENTORY_RESPONSE2"
echo ""

# 测试取消订单
echo "7. 测试取消订单..."
CANCEL_RESPONSE=$(curl -s -X POST $BASE_URL_ORDER/api/orders/ORDER_001/cancel \
  -H "Content-Type: application/json" \
  -d '{
    "reason": "客户主动取消"
  }')

echo "取消订单响应: $CANCEL_RESPONSE"
echo ""

# 等待事件处理
echo "8. 等待事件处理..."
sleep 3

# 最终查看库存状态
echo "9. 最终查看库存状态..."
INVENTORY_RESPONSE3=$(curl -s $BASE_URL_INVENTORY/api/inventory/status)
echo "取消后库存状态: $INVENTORY_RESPONSE3"
echo ""

echo "=== 测试完成 ==="

